package com.lhy.mymall.utils;


import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import java.util.*;

/**
 * @author: 卢辉宇
 * @date: 2023/1/13 11:24
 * @description: Email工具类，用于检验邮件地址是否有效
 */
public class EmailUtil {

    /**
     * 验证邮箱有效性
     *
     * @param email
     * @return
     */
    public static boolean isValidEmailAddress(String email) {
        boolean result = true;
        try {
            /*把传入的email做参数实例化对象*/
            InternetAddress internetAddress = new InternetAddress(email);
            /*验证邮箱地址有效性*/
            internetAddress.validate();
        } catch (AddressException e) {
            e.printStackTrace();
            result = false;
        }
        return result;
    }

    /**
     * 生成随机邮箱验证码
     * @return
     */
    public static String genVerificationCode() {
        List<String> verificationChars = Arrays.asList(new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "N", "M", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "S", "Y", "Z"});
        //利用Collections.shuffle()方法打散列表集合中的字符
        Collections.shuffle(verificationChars);

        String result="";
        //获取打散了的列表中的前6个字符
        for (int i=0;i<6;i++){
            result += verificationChars.get(i);
        }
        return result;
    }


}
